﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Window.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
#endregion

    public class Window : ContainerWidget
    {
        #region Member Variables
        private RectangleF titleBarRectangle;
        private bool mouseDown;
        private bool startDrag;
        private Vector2 mouseOffset;
        #endregion

        #region Constructors
        public Window(GuiManager manager, string name)
            : base(manager, name)
        {
        }

        public Window(GuiManager manager, string name, string title)
            : base(manager, name)
        {
            Text = title;
        }

        #endregion

        public override bool CanFocus
        {
            get
            {
                return true;
            }
        }

        public override void DrawText(IGuiRenderer renderer, Layer layer, TextLabel te)
        {
            if (te.Name.Equals("main", StringComparison.InvariantCultureIgnoreCase))
            {
                Vector2 stringSize = renderer.GuiManager.DefaultFont.MeasureString(Text);
                RectangleF titleRect = new RectangleF(
                                                      (int)(this.ClientRectangle.X + (this.ClientRectangle.Width * 0.5f) - (stringSize.X * 0.5f)),
                                                      this.DisplayRectangle.Top + 4, 
                                                      stringSize.X,
                                                      stringSize.Y);
                renderer.DrawString(renderer.GuiManager.DefaultFont, Text, titleRect.TopLeft, ForeColor);
            }
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            this.Focused = true;

            this.mouseDown = e.LeftButton;
            if (this.mouseDown)
            {
                this.mouseOffset = this.DisplayRectangle.TopLeft - e.Position;
            }

            base.OnMouseDown(sender, e);
        }

        public override void OnGlobalMouseMove(object sender, MouseEventArgs e)
        {
            if (this.mouseDown && !this.startDrag)
            {
                this.startDrag = true;
            }
            else if (this.startDrag && this.Focused)
            {
                Vector2 size = this.DisplayRectangle.Size;
                this.DisplayRectangle = new RectangleF(this.mouseOffset + e.Position, size);
                this.mouseOffset = this.DisplayRectangle.TopLeft - e.Position;
            }

            base.OnGlobalMouseMove(sender, e);
        }

        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            /*
             * Old Draging Code:
            //Console.WriteLine("MouseMove:" + e.ToString() + " DisplayRectangle: " + this.DisplayRectangle.ToString());

            if (mouseDown && !startDrag)
            {
                //if (titleBarRectangle.Contains(e.Position))
                //{
                    startDrag = true;
                //}
            }
            else if (startDrag)
            {
                Vector2 size = this.DisplayRectangle.Size;
                this.DisplayRectangle = new RectangleF(mouseOffset + e.Position, size);
                mouseOffset = this.DisplayRectangle.TopLeft - e.Position;
                
                //this.titleBarRectangle = this.DisplayRectangle;
                //titleBarRectangle.Inflate(-2f, -2f);
                //titleBarRectangle.Height = 28f;
            }

            //foreach (Widget w in Widgets)
            //{
            //    if (w.HitTest(e.Position))
            //    {
            //        w.OnMouseMove(w, e);
            //    }
            //}

            //Console.WriteLine("MouseMove2:" + e.ToString() + " DisplayRectangle: " + this.DisplayRectangle.ToString());
            */
            base.OnMouseMove(sender, e);
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            if ((this.mouseDown || this.startDrag) && e.LeftButton)
            {
                this.mouseDown = false;
                this.startDrag = false;
            }

            base.OnMouseUp(sender, e);
        }

        public override void OnMouseLeave(object sender, EventArgs e)
        {
            this.startDrag = false;
            this.mouseDown = false;

            base.OnMouseLeave(sender, e);
        }

        public override void OnGotFocus(object sender, EventArgs e)
        {
            base.OnGotFocus(sender, e);

            SetState("Focus");
        }

        public override void OnLostFocus(object sender, EventArgs e)
        {
            SetState("Normal");

            base.OnLostFocus(sender, e);
        }
    }
}
